package cn.hp.service.impl;

import cn.hp.entity.Menu;
import cn.hp.entity.User;
import cn.hp.mapper.MenuMapper;
import cn.hp.mapper.UserMapper;
import cn.hp.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
    public class UserServiceImpl implements UserService {
        @Resource
        private UserMapper userMapper;
        private MenuMapper menuMapper;

        @Override
        public User doLogin(User user) {
            List<User> list = userMapper.findUserByEmailAndPassword(user);
            return list.size() > 0 ? list.get(0) : null;
        }

        @Override
        public User selectUserById(Integer id) {
            return userMapper.selectUserById(id);
        }

        /**
         * 分页查询列表
         *
         * @param user
         * @return
         */
        @Override
        public PageInfo<User> selectUserList(User user, Integer page, Integer limit) {
            PageHelper.startPage(page, limit);
            List<User> list = userMapper.selectUserList(user);
            return new PageInfo<User>(list);
        }

        /**
         * 查询列表
         *
         * @param user
         * @return
         */
        @Override
        public List<User> selectUserList(User user) {
            return userMapper.selectUserList(user);
        }

        /**
         * 新增
         *
         * @param user
         * @return 结果
         */
        @Override
        @Transactional
        public int insertUser(User user) {
            return userMapper.insertUser(user);
        }

        /**
         * 修改
         *
         * @param user
         * @return 结果
         */
        @Override
        @Transactional
        public int updateUser(User user) {
            return userMapper.updateUser(user);
        }

        /**
         * 删除信息
         *
         * @param id ID
         * @return 结果
         */
        @Override
        @Transactional
        public int deleteUserById(Integer id) {
            return userMapper.deleteUserById(id);
        }

        @Override
        public int resetPassword(User user) {
            return userMapper.updateUser(user);
        }

        @Override
        public Boolean checkEmail(String email) {
            Map map = userMapper.checkEmail(email);
            Integer num = Integer.valueOf(map.get("num").toString());
            return num > 0 ? true : false;
        }

    @Override
    public List<Menu> selectUidMenu(User user) {
        return menuMapper.selectUidMenu(user);
    }
}

